package com.mission.schedule.receiver;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;

import java.io.File;

/**
 * Created by liwei on 2017/8/1.
 */

public class DownloadManagerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
//        long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        SharedPreferences sPreferences = context.getSharedPreferences("downloadplato", 0);

        String refernece = sPreferences.getString("plato", "");

        if (!refernece .isEmpty()) {

//            String serviceString = Context.DOWNLOAD_SERVICE;
//
//            DownloadManager dManager = (DownloadManager) context.getSystemService(serviceString);

            Intent install = new Intent(Intent.ACTION_VIEW);

//            Uri downloadFileUri = dManager.getUriForDownloadedFile(myDwonloadID);

//            install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            install.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/"+refernece)),
                    "application/vnd.android.package-archive");
            context.startActivity(install);
            android.os.Process.killProcess(android.os.Process.myPid());
        }

    }
}
